import math
class WGS84toGCJ02:
    def __init__(self) -> None:
        self.pi = 3.1415926535897932384626
        self.a = 6378245.0
        self.ee = 0.00669342162296594323
    
    def wgs84_gcj02(self, coordinate):
        wgs84_longitude = coordinate[0]
        wgs84_latitude = coordinate[1]

        dLat = self.__transform_lat(wgs84_longitude - 105.0, wgs84_latitude - 35.0)
        dLon = self.__transform_lon(wgs84_longitude - 105.0, wgs84_latitude - 35.0)
        radLat = wgs84_latitude / 180.0 * self.pi
        magic = math.sin(radLat)
        magic = 1 - self.ee * magic * magic
        sqrtMagic = math.sqrt(magic)
        dLat = (dLat * 180.0) / ((self.a * (1 - self.ee)) / (magic * sqrtMagic) * self.pi)
        dLon = (dLon * 180.0) / (self.a / sqrtMagic * math.cos(radLat) * self.pi)
        mgLat = wgs84_latitude + dLat
        mgLon = wgs84_longitude + dLon
        return [mgLon,mgLat]
    
    def __transform_lat(self,x, y):
        result = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
        result += (20.0 * math.sin(6.0 * x * self.pi) + 20.0 * math.sin(2.0 * x * self.pi)) * 2.0 / 3.0
        result += (20.0 * math.sin(y * self.pi) + 40.0 * math.sin(y / 3.0 * self.pi)) * 2.0 / 3.0
        result += (160.0 * math.sin(y / 12.0 * self.pi) + 320 * math.sin(y * self.pi / 30.0)) * 2.0 / 3.0
        return result


    def __transform_lon(self,x, y):
        result = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
        result += (20.0 * math.sin(6.0 * x * self.pi) + 20.0 * math.sin(2.0 * x * self.pi)) * 2.0 / 3.0
        result += (20.0 * math.sin(x * self.pi) + 40.0 * math.sin(x / 3.0 * self.pi)) * 2.0 / 3.0
        result += (150.0 * math.sin(x / 12.0 * self.pi) + 300.0 * math.sin(x / 30.0 * self.pi)) * 2.0 / 3.0
        return result


if __name__ == "__main__":
    trans = WGS84toGCJ02()
    print(trans.wgs84_gcj02([119.5,35.5]))